/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: PushUtils.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	20 Jul,2015
 */
package com.sirdc.modules.push.util;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.push.entity.ChatroomInfo;
import com.sirdc.modules.push.entity.GroupInfo;
import com.sirdc.modules.push.entity.Message;
import com.sirdc.modules.push.entity.SdkHttpResult;

/**
 * 
 * @version Revision: 0.0.1
 * @Date: 20 Jul,2015
 */
public class PushApiUtils {

	private static final String UTF8 = "UTF-8";
	private static final String RONG_CLOUD_URI = "http://api.cn.ronghub.com";
	private static final String GET_TOKEN_URI = "/user/getToken.json";//获取token
	private static final String CHECK_ONLINE_URI = "/user/checkOnline.json";//检测用户是否在线
	private static final String REFRESH_TOKEN_URI = "/user/refresh.json";//刷新用户信息
	
	private static final String BLOCK_USER_URI = "/user/block.json";//系统封禁用户
	private static final String UNBLOCK_USER_URI = "/user/unblock.json";//系统解禁用户
	private static final String BOLOCK_USER_QUERY_URI = "/user/block/query.json";//获取被系统封禁用户
	
	private static final String BOLOCK_PERSON_URI = "/user/blacklist/add.json";//添加用户到黑名单
	private static final String UNBOLOCK_PERSON_URI = "/user/blacklist/remove.json";//从黑名单移除用户
	private static final String BOLOCK_PERSON_QUERY_URI = "/user/blacklist/query.json";//获取黑名单用户
	
	private static final String CREATE_GROUP_URI = "/group/create.json";//创建群
	private static final String JOIN_GROUP_URI = "/group/join.json";//加入群
	private static final String QUIT_GROUP_URI = "/group/quit.json";//退出群
	private static final String DISMISS_GROUP_URI = "/group/dismiss.json";//解散群
	private static final String SYNC_GROUP_URI = "/group/sync.json";//同步群用户信息
	private static final String REFRESH_GROUP_URI = "/group/refresh.json";//同步群信息
	
	private static final String PUBLISH_PRIVATE_MESSAGE_URI = "/message/private/publish.json";//发布个人消息
	private static final String PUBLISH_SYSTEM_MESSAGE_URI = "/message/system/publish.json";//发布系统消息
	private static final String PUBLISH_GROUP_MESSAGE_URI = "/message/group/publish.json";//发布群消息
	private static final String PUBLISH_CHATROOM_MESSAGE_URI = "/message/chatroom/publish.json";//发布聊天室消息
	private static final String PUBLISH_BROADCAST_MESSAGE_URI = "/message/broadcast.json";//发布广播消息
	
	private static final String CREATE_CHATROOM_URI = "/chatroom/create.json";//创建聊天室
	private static final String DESTORY_CHATROOM_URI = "/chatroom/destroy.json";//销毁聊天室
	private static final String QUERY_CHATROOM_URI = "/chatroom/query.json";//查询聊天室
	
	private static final String QUERY_MESSAGE_HISTORY_URI = "/message/history.json";//查询消息历史
	private static final String DELETE_MESSAGE_HISTORY_URI = "/message/history.json";//删除消息历史
	
	//获取token
	public static SdkHttpResult getToken(String appKey, String appSecret, String userId, String userName, String portraitUri) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		formMap.put("name", URLEncoder.encode(userName, UTF8));
		formMap.put("portraitUri", URLEncoder.encode(portraitUri, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + GET_TOKEN_URI, formMap);
	}
	
	//检查用户在线状态
	public static SdkHttpResult checkOnline(String appKey, String appSecret, String userId) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + CHECK_ONLINE_URI, formMap);
	}
	
	//刷新用户信息
	public static SdkHttpResult refreshUser(String appKey, String appSecret, String userId, String userName, String portraitUri) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		formMap.put("name", URLEncoder.encode(userName, UTF8));
		formMap.put("portraitUri", URLEncoder.encode(portraitUri, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + REFRESH_TOKEN_URI, formMap);
	}
	
	//封禁用户
	public static SdkHttpResult blockUser(String appKey, String appSecret, String userId, int minute) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		formMap.put("minute", URLEncoder.encode(String.valueOf(minute), UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + BLOCK_USER_URI, formMap);
	}
	
	//解禁用户
	public static SdkHttpResult unblockUser(String appKey, String appSecret, String userId) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + UNBLOCK_USER_URI, formMap);
	}

	//获取被封禁用户
	public static SdkHttpResult queryBlockUsers(String appKey, String appSecret) throws Exception {
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + BOLOCK_USER_QUERY_URI, null);
	}
	
	//添加用户到黑名单
	public static SdkHttpResult blackUser(String appKey, String appSecret, String userId, List<String> blackUserIds) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		formMap.put("blackUserId", blackUserIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + BOLOCK_PERSON_URI, formMap);
	}
	
	//从黑名单移除用户
	public static SdkHttpResult unblackUser(String appKey, String appSecret, String userId, List<String> blackUserIds) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		formMap.put("blackUserId", blackUserIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + UNBOLOCK_PERSON_URI, formMap);
	}
	
	//获取黑名单用户
	public static SdkHttpResult queryblackUser(String appKey, String appSecret, String userId) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + BOLOCK_PERSON_QUERY_URI, formMap);
	}
	
	//创建群
	public static SdkHttpResult createGroup(String appKey, String appSecret, List<String> userIds, String groupId, String groupName) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("groupName", URLEncoder.encode(groupName, UTF8));
		formMap.put("userId", userIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + CREATE_GROUP_URI, formMap);
	}

	//创建群
	public static SdkHttpResult joinGroup(String appKey, String appSecret, String userId, String groupId, String groupName) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("groupName", URLEncoder.encode(groupName, UTF8));
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + JOIN_GROUP_URI, formMap);
	}
	
	//创建群
	public static SdkHttpResult joinGroup(String appKey, String appSecret, List<String> userIds, String groupId, String groupName) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("groupName", URLEncoder.encode(groupName, UTF8));
		formMap.put("userId", userIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + JOIN_GROUP_URI, formMap);
	}

	//退出群
	public static SdkHttpResult quitGroup(String appKey, String appSecret, String userId, String groupId) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + QUIT_GROUP_URI, formMap);
	}
	
	//退出群
	public static SdkHttpResult quitGroup(String appKey, String appSecret, List<String> userIds, String groupId) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("userId", userIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + QUIT_GROUP_URI, formMap);
	}
	
	//解散群
	public static SdkHttpResult dismissGroup(String appKey, String appSecret, String userId, String groupId) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + DISMISS_GROUP_URI, formMap);
	}
	
	//同步用户群信息
	public static SdkHttpResult syncGroup(String appKey, String appSecret, String userId, List<GroupInfo> groups) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("userId", URLEncoder.encode(userId, UTF8));
		if (groups != null) {
			for (GroupInfo info : groups) {
				if (info != null) {
					String key = String.format("group[%s]", URLEncoder.encode(info.getId(), UTF8));
					String val = URLEncoder.encode(info.getName(), UTF8);
					formMap.put(key, val);
				}
			}
		}
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + SYNC_GROUP_URI, formMap);
	}
	
	//同步群信息
	public static SdkHttpResult refreshGroupInfo(String appKey, String appSecret, String groupId, String groupName) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("groupId", URLEncoder.encode(groupId, UTF8));
		formMap.put("groupName", URLEncoder.encode(groupName, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + REFRESH_GROUP_URI, formMap);
	}
	
	//发送消息
	public static SdkHttpResult publishMessage(String appKey, String appSecret, String fromUserId, List<String> toUserIds, Message msg) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("fromUserId", URLEncoder.encode(fromUserId, UTF8));
		formMap.put("objectName", URLEncoder.encode(msg.getType(), UTF8));
		formMap.put("content", URLEncoder.encode(msg.toString(), UTF8));
		formMap.put("toUserId", toUserIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + PUBLISH_PRIVATE_MESSAGE_URI, formMap);
	}
	
	//发送消息
	public static SdkHttpResult publishMessage(String appKey, String appSecret, String fromUserId, List<String> toUserIds, Message msg, String pushContent, String pushData) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("fromUserId", URLEncoder.encode(fromUserId, UTF8));
		formMap.put("objectName", URLEncoder.encode(msg.getType(), UTF8));
		formMap.put("content", URLEncoder.encode(msg.toString(), UTF8));
		formMap.put("toUserId", toUserIds);
		formMap.put("pushContent", URLEncoder.encode(pushContent, UTF8));
		formMap.put("pushData", URLEncoder.encode(pushData, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + PUBLISH_PRIVATE_MESSAGE_URI, formMap);
	}
	
	//发送系统消息
	public static SdkHttpResult publishSystemMessage(String appKey, String appSecret, String fromUserId, List<String> toUserIds, Message msg, String pushContent, String pushData) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("fromUserId", URLEncoder.encode(fromUserId, UTF8));
		formMap.put("objectName", URLEncoder.encode(msg.getType(), UTF8));
		formMap.put("content", URLEncoder.encode(msg.toString(), UTF8));
		formMap.put("toUserId", toUserIds);
		formMap.put("pushContent", URLEncoder.encode(pushContent, UTF8));
		formMap.put("pushData", URLEncoder.encode(pushData, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + PUBLISH_SYSTEM_MESSAGE_URI, formMap);
	}
	
	//发送群消息
	public static SdkHttpResult publishGroupMessage(String appKey, String appSecret, String fromUserId, List<String> toGroupIds, Message msg, String pushContent, String pushData) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("fromUserId", URLEncoder.encode(fromUserId, UTF8));
		formMap.put("objectName", URLEncoder.encode(msg.getType(), UTF8));
		formMap.put("content", URLEncoder.encode(msg.toString(), UTF8));
		formMap.put("pushContent", URLEncoder.encode(pushContent, UTF8));
		formMap.put("pushData", URLEncoder.encode(pushData, UTF8));
		formMap.put("toGroupId", toGroupIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + PUBLISH_GROUP_MESSAGE_URI, formMap);
	}
	
	//发送聊天室消息
	public static SdkHttpResult publishChatroomMessage(String appKey, String appSecret, String fromUserId, List<String> toChatroomIds, Message msg) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("fromUserId", URLEncoder.encode(fromUserId, UTF8));
		formMap.put("objectName", URLEncoder.encode(msg.getType(), UTF8));
		formMap.put("content", URLEncoder.encode(msg.toString(), UTF8));
		formMap.put("toChatroomId", toChatroomIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + PUBLISH_CHATROOM_MESSAGE_URI, formMap);
	}

	//发送广播消息
	public static SdkHttpResult broadcastMessage(String appKey, String appSecret, String fromUserId, Message msg,String pushContent, String pushData) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("fromUserId", URLEncoder.encode(fromUserId, UTF8));
		formMap.put("objectName", URLEncoder.encode(msg.getType(), UTF8));
		formMap.put("content", URLEncoder.encode(msg.toString(), UTF8));
		formMap.put("pushContent", URLEncoder.encode(pushContent, UTF8));
		formMap.put("pushData", URLEncoder.encode(pushData, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + PUBLISH_BROADCAST_MESSAGE_URI, formMap);
	}
	
	//创建聊天室
	public static SdkHttpResult createChatroom(String appKey, String appSecret, List<ChatroomInfo> chatrooms) throws Exception { 
		Map<String, Object> formMap = new HashMap<String, Object>();
		if (chatrooms != null) {
			for (ChatroomInfo info : chatrooms) {
				if (info != null) {
					String key = String.format("chatroom[%s]", URLEncoder.encode(info.getId(), UTF8));
					String val = URLEncoder.encode(info.getName(), UTF8);
					formMap.put(key, val);
				}
			}
		}
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + CREATE_CHATROOM_URI, formMap);
	}
	
	//销毁聊天室
	public static SdkHttpResult destroyChatroom(String appKey, String appSecret, List<String> chatroomIds) throws Exception { 
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("chatroomId", chatroomIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + DESTORY_CHATROOM_URI, formMap);
	}
	
	//查询聊天室信息
	public static SdkHttpResult queryChatroom(String appKey, String appSecret, List<String> chatroomIds) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("chatroomId", chatroomIds);
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + QUERY_CHATROOM_URI, formMap);
	}
	
	//获取消息历史记录下载地址
	public static SdkHttpResult getMessageHistoryUrl(String appKey, String appSecret, String date) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("date", URLEncoder.encode(date, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + QUERY_MESSAGE_HISTORY_URI, formMap);
	}
	
	//删除消息历史记录
	public static SdkHttpResult deleteMessageHistory(String appKey, String appSecret, String date) throws Exception {
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("date", URLEncoder.encode(date, UTF8));
		return PushHttpUtils.sendPost(appKey, appSecret, RONG_CLOUD_URI + DELETE_MESSAGE_HISTORY_URI, formMap);
	}
}
